package beautiful.butterfly.schedule.test;

import beautiful.butterfly.schedule.MultipleTaskDeal;
import beautiful.butterfly.schedule.TaskItemAndParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;


public class MultipleTaskDealDemo implements MultipleTaskDeal<Long> {
    protected static transient Logger log = LoggerFactory.getLogger(MultipleTaskDealDemo.class);

    public Comparator<Long> getComparator() {
        return new Comparator<Long>() {
            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }

            public boolean equals(Object obj) {
                return this == obj;
            }
        };
    }

    public List<Long> getTaskList(String taskParameter, String ownSign, int taskItemNum,
                                  int fetchNum, List<TaskItemAndParameter> taskItemAndParameterList) throws Exception {
        List<Long> result = new ArrayList<Long>();
        int num = fetchNum / taskItemAndParameterList.size();
        Random random = new Random(System.currentTimeMillis());
        String message = "...[ownSign=" + ownSign + ",taskParameter=\"" + taskParameter + "\"]:";
        boolean isFirst = true;
        for (TaskItemAndParameter taskItemAndParameter : taskItemAndParameterList) {
            long taskItem = Long.parseLong(taskItemAndParameter.getTaskItem()) * 10000000L;
            for (int i = 0; i < num; i++) {
                result.add(taskItem + random.nextLong() % 100000L);
            }
            if (isFirst == false) {
                message = message + ",";
            } else {
                isFirst = false;
            }
            message = message + taskItemAndParameter.getTaskItem() + "{" + taskItemAndParameter.getParameter() + "}";
        }
        log.info(message);
        return result;
    }

    public boolean execute(Long[] tasks, String ownSign) throws Exception {
        Thread.sleep(50);

        return true;
    }

}